<?php

namespace app\admin\model\zhongtai;

use think\Model;

class ShoproOrderBillLog extends Model
{
    // 表名
    protected $table = 'shopro_order_bill_log';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $deleteTime = false;

    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $order_data = (new ShoproOrder())->getOrderSnStatus($manage_id,$v['order_sn']);
                if(!$order_data) continue;
                $in_data = [
                    "manage_id" => $manage_id,
                    "bill_id" => $v['bill_id'],
                    "bill_log_id" => $v['id'],
                    "order_sn" => $v['order_sn'],
                    "chnltrxid" => $v['chnltrxid'],
                    "mchId" => $v['mchId'],
                    "fz_account" => $v['fz_account'],
                    "fz_number" => $v['fz_number'],
                    "recharge_order_sn" => $v['recharge_order_sn'],
                    "recharge_id" => $v['recharge_id'],
                    "fen_fee" => $v['fen_fee'],
                    "all_fee" => $v['all_fee'],
                    "sup_fee" => $v['sup_fee'],
                    "is_send" => $v['is_send'],
                    "is_success" => $v['is_success'],
                    "fen_sn" => $v['fen_sn'],
                    "req_json" => $v['req_json'],
                    "check_json" => $v['check_json'],
                    "is_check" => $v['is_check'],
                    "is_two" => $v['is_two'],
                    "zt_time" => $zttime,
                ];
                $user_data = $this->where([
                    'bill_log_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'bill_log_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "shopro_order_bill_log表更新完成";
    }
}
